package org.swift.commons.xml;

import java.util.Map;

import java.util.Collections;

public class ValueMatch implements Match {

	private final String value;

	private final int offset;

	private final int length;

	private NamespaceDefiner definer;
	
	public ValueMatch(String singleValue) {
		this.value = singleValue;
		this.offset = -1;
		this.length = singleValue.length();		
		this.definer = new NamespaceDefiner(Collections.<String, String>emptyMap());
	}
	
	public ValueMatch(String singleValue, Map<String, String> namespaces) {
		this.value = singleValue;
		this.offset = -1;
		this.length = singleValue.length();		
		this.definer = new NamespaceDefiner(namespaces);
	}

	public ValueMatch(String singleValue, int offset,
			Map<String, String> namespaces) {
		this.value = singleValue;
		this.offset = offset;
		this.length = singleValue.length();
		this.definer = new NamespaceDefiner(namespaces);
	}

	public String getValue() {
		return getValue(true);
	}

	/**
	 * returns the length of the match
	 * 
	 */
	@Override
	public int getLength() {
		return this.length;
	}

	/**
	 * returns the starting byte location of the match
	 * 
	 */
	@Override
	public int getPosition() {
		return this.offset;
	}

	@Override
	public boolean isIndexed() {
		return false;
	}

	@Override
	public String getValue(boolean withNamespaces) {
		if (withNamespaces) {
			return this.definer.defineNamespaces(this.value);
		} else {
			return this.value;
		}
	}

	protected void setDefiner(NamespaceDefiner definer) {
		this.definer = definer;
	}


	
}
